// 更新组件导入路径脚本
const fs = require('fs');
const path = require('path');

// 组件路径映射
const componentPathMapping = {
  '@/app/components/error': '@/app/components/common/error',
  '@/app/components/loading': '@/app/components/common/loading',
  '@/app/components/html-link': '@/app/components/common/html-link',
  '@/app/components/history-listener': '@/app/components/common/history-listener',
  '@/app/components/enhanced-filter': '@/app/components/filter/enhanced-filter',
  '@/app/components/video-container': '@/app/components/video/video-container',
  '@/app/components/video-player': '@/app/components/video/video-player',
  '@/app/components/video-skeleton': '@/app/components/video/video-skeleton',
  '@/app/components/responsive-video-player': '@/app/components/video/responsive-video-player',
  '@/app/components/video-performance-debug': '@/app/components/video/video-performance-debug',
  '@/app/components/mobile-nav': '@/app/components/navigation/mobile-nav',
  '@/app/components/nav-bar': '@/app/components/navigation/nav-bar',
  '@/app/components/footer': '@/app/components/navigation/footer',
  '@/app/components/pagination': '@/app/components/navigation/pagination',
  '@/app/components/videos': '@/app/components/video-list/videos',
  '@/app/components/video-card': '@/app/components/video-list/video-card',
  '@/app/components/carousel': '@/app/components/video-list/carousel',
  '@/app/components/category-section': '@/app/components/video-list/category-section',
  '@/app/components/playlist-container': '@/app/components/playlist/playlist-container',
  '@/app/components/mobile-playlist': '@/app/components/playlist/mobile-playlist',
  '@/app/components/filter-section': '@/app/components/filter/filter-section',
  '@/app/components/vip-content': '@/app/components/vip/vip-content',
  '@/app/components/vip-only-tip': '@/app/components/vip/vip-only-tip',
  '@/app/components/client-vip-tip': '@/app/components/vip/client-vip-tip',
  '@/app/components/rem-adapter': '@/app/components/common/rem-adapter'
};

// 相对路径组件映射
const relativePathMapping = {
  './components/error': './components/common/error',
  './components/loading': './components/common/loading',
  './components/html-link': './components/common/html-link',
  './components/history-listener': './components/common/history-listener',
  './components/enhanced-filter': './components/filter/enhanced-filter',
  './components/video-container': './components/video/video-container',
  './components/video-player': './components/video/video-player',
  './components/video-skeleton': './components/video/video-skeleton',
  './components/responsive-video-player': './components/video/responsive-video-player',
  './components/video-performance-debug': './components/video/video-performance-debug',
  './components/mobile-nav': './components/navigation/mobile-nav',
  './components/nav-bar': './components/navigation/nav-bar',
  './components/footer': './components/navigation/footer',
  './components/pagination': './components/navigation/pagination',
  './components/videos': './components/video-list/videos',
  './components/video-card': './components/video-list/video-card',
  './components/carousel': './components/video-list/carousel',
  './components/category-section': './components/video-list/category-section',
  './components/playlist-container': './components/playlist/playlist-container',
  './components/mobile-playlist': './components/playlist/mobile-playlist',
  './components/filter-section': './components/filter/filter-section',
  './components/vip-content': './components/vip/vip-content',
  './components/vip-only-tip': './components/vip/vip-only-tip',
  './components/client-vip-tip': './components/vip/client-vip-tip',
  './components/rem-adapter': './components/common/rem-adapter'
};

// 获取所有TypeScript和TSX文件
const getAllFiles = (dir, fileList = []) => {
  const files = fs.readdirSync(dir);
  
  files.forEach(file => {
    const filePath = path.join(dir, file);
    const stat = fs.statSync(filePath);
    
    if (stat.isDirectory() && !filePath.includes('node_modules') && !filePath.includes('.next')) {
      fileList = getAllFiles(filePath, fileList);
    } else if ((filePath.endsWith('.ts') || filePath.endsWith('.tsx') || filePath.endsWith('.js') || filePath.endsWith('.jsx')) && 
               !filePath.includes('node_modules') && !filePath.includes('.next')) {
      fileList.push(filePath);
    }
  });
  
  return fileList;
};

// 更新文件中的导入路径
const updateImports = (filePath) => {
  let content = fs.readFileSync(filePath, 'utf8');
  let updated = false;
  
  // 检查并替换绝对导入路径
  Object.keys(componentPathMapping).forEach(oldPath => {
    const newPath = componentPathMapping[oldPath];
    const regex = new RegExp(`from ['"](${oldPath})['"]`, 'g');
    
    if (regex.test(content)) {
      content = content.replace(regex, `from '${newPath}'`);
      updated = true;
    }
  });
  
  // 检查并替换相对导入路径
  Object.keys(relativePathMapping).forEach(oldPath => {
    const newPath = relativePathMapping[oldPath];
    const regex = new RegExp(`from ['"](${oldPath})['"]`, 'g');
    
    if (regex.test(content)) {
      content = content.replace(regex, `from '${newPath}'`);
      updated = true;
    }
  });
  
  // 如果文件被更新，则写回
  if (updated) {
    fs.writeFileSync(filePath, content, 'utf8');
    console.log(`更新了组件导入: ${filePath}`);
  }
};

// 主函数
const main = () => {
  console.log('开始更新组件导入路径...');
  
  // 获取项目根目录下的所有TypeScript和TSX文件
  const files = getAllFiles(path.resolve(__dirname, '..'));
  
  // 更新每个文件中的导入路径
  files.forEach(file => {
    updateImports(file);
  });
  
  console.log('组件导入路径更新完成！');
};

main(); 